﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
using Unbound.ValueUnbinders;
using DddCms.Presentation.Areas.DddCms.Models.UIComponent.Grid;
using Unbound;

namespace DddCms.Presentation.UIComponent.Grid
{
    public static class SortingForExtensions
    {
        public static SortingViewModel SortingFor<TModel, TValue>(
                             this HtmlHelper<TModel> html,
                             Expression<Func<TModel, TValue>> expression)
        {
            var x = ModelMetadata.FromLambdaExpression<TModel, TValue>(expression, html.ViewData);
            if (x.AdditionalValues.ContainsKey("Sorting"))
            {
                SortingViewModel model = x.AdditionalValues["Sorting"] as SortingViewModel;
                return model;
            }
            return new SortingViewModel();
        }

        public static IDictionary<string, object> ToHttpDictionary<T>
          (this T model, params Expression<Func<T, object>>[] propertiesToSkip)
        {
            return new Unbinder().Unbind(x => x.Source(model).SkipProperties(propertiesToSkip));
        }
    }


}